package com.talking.view.main;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.bumptech.glide.Glide;
import com.talking.App;
import com.talking.R;
import com.talking.databinding.FragmentMeBinding;
import com.talking.model.Http;
import com.talking.presenter.UserPresenter;
import com.talking.utils.Utils;
import com.talking.view.AccountActivity;
import com.talking.view.LoginActivity;
import com.talking.view.MomentActivity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MeFragment extends Fragment {
    private FragmentMeBinding binding;
    private MainActivity activity;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentMeBinding.inflate(inflater, container, false);
        activity= (MainActivity) getActivity();
        View root = binding.getRoot();
        binding.meOpenAccound.setOnClickListener(v ->{
          startActivityForResult(new Intent(requireActivity(), AccountActivity.class),1);
        });
        binding.meEmail.setText(App.sp.getString("email","邮箱错误"));
        binding.meMoment.setOnClickListener(v -> {
            App.momentAccount="";
            startActivity(new Intent(requireActivity(), MomentActivity.class));
        });

        binding.meMomentPerson.setOnClickListener(v -> {
            App.momentAccount=App.sp.getString("account","");
            startActivity(new Intent(requireActivity(), MomentActivity.class));
        });
        return root;
    }
    @Override
    public void onStart() {
        super.onStart();
        getMsg();
    }
    private void getMsg() {
        UserPresenter.getUserByAccount(App.sp.getString("account", ""),null, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                requireActivity().runOnUiThread(() -> binding.meNickname.setText("还没有昵称哦"));
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                requireActivity().runOnUiThread(() -> {
                    try {
                        JSONObject jo = new JSONObject(response.body().string());
                        if (!jo.isNull("nickname")){
                            binding.meNickname.setText(jo.getString("nickname"));
                            Glide.with(activity).load(Http.BASE_URL+jo.getString("iconUrl")).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.meIcon);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            getMsg();
        }
        if (requestCode==1&&resultCode==3){
            @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
            edit.putBoolean("autoLogin",false);
            edit.apply();
            startActivity(new Intent(requireActivity(), LoginActivity.class));
            requireActivity().finish();
        }
    }
}